package controllers

import (
	"encoding/json"
	_ "gitchina/kly_service/errors"
	"gitchina/kly_service/models"
	_ "gitchina/kly_service/util"
	"github.com/astaxie/beego"
)

const (
	login_token = `login_token`
)

type MemberController struct {
	beego.Controller
}

// @router /101000 [get]
func (m *MemberController) Welcome() {
	m.Data["json"] = "welcome"
	m.ServeJSON()
}

// @router /101001 [post]
func (m *MemberController) AddMember() {
	var rq models.AddMemberRQ
	var mbr models.Member
	var auth models.Auth
	err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rq.ImageRootDir = beego.AppConfig.String("imageDir")
		rq.ImageCdn = beego.AppConfig.String("imageCdn")
		if _, err := mbr.AddMember(&rq); err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			mbrInfo, err := auth.Login(rq.Phone, rq.Passwd, rq.RegisterId, rq.Platform)
			if err != nil {
				m.Data["json"] = models.NewFailResponse(err)
			} else {
				m.Data["json"] = models.NewOkResponse(mbrInfo)
			}
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101003 [post]
func (m *MemberController) GetMemberInfo() {
	var rq models.GetMemberInfoRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		if rs, err := mbr.GetMemberInfo(rq.MemberId); err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(rs)
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101004 [post]
func (m *MemberController) Login() {
	var rq models.LoginRQ
	var auth models.Auth
	var mbr models.Member

	cdn := beego.AppConfig.String("qrcodeCdn")
	dir := beego.AppConfig.String("qrcodeDir")
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		mbrInfo, err := auth.Login(rq.Phone, rq.Passwd, rq.RegisterId, rq.Platform)
		if err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(mbrInfo)
		}

		if !mbrInfo.QRCodeExists && err == nil {
			mbr.AddQRCode(cdn, string(mbrInfo.Id), dir, mbrInfo.RawId)
		}

		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101005 [post]
func (m *MemberController) UpdatePasswd() {
	var rq models.UpdPasswdRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		err := mbr.UpdatePasswd(rq.Phone, rq.Code, rq.Passwd)
		if err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(nil)
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101006 [post]
func (m *MemberController) SendRegistryVerifyCode() {
	var rq models.SendRegistryVerifyCodeRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		if err := mbr.SendRegistryVerifyCode(rq.Phone); err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(nil)
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101007 [post]
func (m *MemberController) UpdateCarrierOwner() {
	var rq models.UpdateCarrierOwnerRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
		err := mbr.UpdateCarrierOwner(mbrId, rq)
		if err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(nil)
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101008 [post]
func (m *MemberController) UpdateCargoOwner() {
	var rq models.UpdateCargoOwnerRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
		err := mbr.UpdateCargoOwner(mbrId, rq)
		if err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(nil)
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101010 [post]
func (m *MemberController) GetCarrierOwner() {
	var mbr models.Member
	mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
	if rs, err := mbr.GetCarrierOwner(mbrId); err != nil {
		m.Data["json"] = models.NewFailResponse(err)
	} else {
		m.Data["json"] = models.NewOkResponse(rs)
	}
	m.ServeJSON()
}

// @router /101011 [post]
func (m *MemberController) GetCargoOwner() {
	var mbr models.Member
	mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
	if rs, err := mbr.GetCargoOwner(mbrId); err != nil {
		m.Data["json"] = models.NewFailResponse(err)
	} else {
		m.Data["json"] = models.NewOkResponse(rs)
	}
	m.ServeJSON()
}

// @router /101012 [post]
func (m *MemberController) CheckLogin() {
	m.Data["json"] = models.NewOkResponse(nil)
	m.ServeJSON()
}

// @router /101013 [post]
func (m *MemberController) Logout() {
	var auth models.Auth
	token := m.Ctx.Request.Header.Get("Auth-Token")
	auth.Logout(token)
	m.Data["json"] = models.NewOkResponse(nil)
	m.ServeJSON()
}

// @router /101014 [post]
func (m *MemberController) GetAuthState() {
	var mbr models.Member
	mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
	if rs, err := mbr.GetAuthState(mbrId); err != nil {
		m.Data["json"] = models.NewFailResponse(err)
	} else {
		m.Data["json"] = models.NewOkResponse(rs)
	}
	m.ServeJSON()
}

// @router /101015 [post]
func (m *MemberController) AddRole() {
	var rq models.AddRoleRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
		err := mbr.AddRole(mbrId, rq.MbrType)
		if err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(nil)
		}
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /101016 [post]
func (m *MemberController) GetIDCard() {
	var mbr models.Member
	mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
	if rs, err := mbr.GetPersonalAuthInfo(mbrId); err != nil {
		m.Data["json"] = models.NewFailResponse(err)
	} else {
		m.Data["json"] = models.NewOkResponse(rs)
	}
	m.ServeJSON()
}

// @router /101017 [post]
func (m *MemberController) UpdateIDAuthState() {
	var rq models.UpdateIDAuthStateRQ
	var mbr models.Member
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		mbrId := models.ID(m.Ctx.Input.GetData("mbrId").(string))
		if err := mbr.UpdateIDAuthState(mbrId, rq.Name, rq.IDCardNo); err != nil {
			m.Data["json"] = models.NewFailResponse(err)
		} else {
			m.Data["json"] = models.NewOkResponse(nil)
		}

		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}
